package at.codebase.web.templates;

import java.util.ArrayList;

import at.codebase.security.SecurePrintWriter;
import at.codebase.security.SecureStringBuilder;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

public class ErrorWidget extends Template{
	
	private ArrayList<String> errorItems = new ArrayList<String>();
	
	private static String TEMPLATE_URL = "error/error_widget.html";
	
	public ErrorWidget() {
		super(TemplateEngine.getTemplateContent(TEMPLATE_URL));
	}
	
	public ErrorWidget(SecurePrintWriter writer) {
		super(TemplateEngine.getTemplateContent(TEMPLATE_URL), writer);
	}
	
	public void addErrorItem(String text){
		errorItems.add(text);
		reAddErrors();
	}

	private void reAddErrors() {
		SecureStringBuilder buffer = new SecureStringBuilder();
		for(String text:errorItems){
			buffer.appendInsecure("<li>");
			buffer.appendInsecure(text);
			buffer.appendInsecure("</li>");
		}
		this.setHTML("errorItems", buffer.toString());
	}
	
	public boolean hasErrors(){
		return (errorItems.size() > 0);
	}
	
}
